<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @selection-change="selectionChange"
    >
      <template slot="menuLeft">
        <el-button
          type="danger"
          size="small"
          icon="el-icon-delete"
          plain
          v-if="permission.riskseverity_delete"
          @click="handleDelete"
          >删 除
        </el-button>
      </template>

      <!-- 财产损失label表单自定义 -->
      <template slot="ccssLabel">
        <label>财产损失:</label>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {
  getList,
  getDetail,
  add,
  update,
  remove
} from "@/api/saferisk/severitystandard";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      form: {},
      query: {},
      data: [],
      loading: true,
      selectionList: [],
      option: {
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        viewBtn: true,
        selection: true,
        refreshBtn: false,
        columnBtn: false,
        dialogClickModal: false,
        defaultSort: { prop: "dj", order: "descending" },
        column: [
          {
            label: "等级",
            prop: "dj",
            align: "center",
            width: 80,
            type: "number",
            minRows: 1,
            maxRows: 5,
            step: 1,
            rules: [
              {
                required: true,
                message: "请输入等级",
                trigger: "blur"
              }
            ]
          },
          {
            label: "法律法规",
            prop: "flfgjqtyq",
            align: "center",
            rules: [
              {
                required: true,
                message: "请输入法律、法规及其他要求",
                trigger: "blur"
              }
            ]
          },
          {
            label: "人员",
            prop: "ry",
            align: "center",
            rules: [
              {
                required: true,
                message: "请输入人员",
                trigger: "blur"
              }
            ]
          },
          {
            label: "财产损失/万元",
            prop: "ccss",
            align: "center",
            append: "万元",
            labelslot: true,
            width: 120,
            rules: [
              {
                required: true,
                message: "请输入财产损失",
                trigger: "blur"
              }
            ]
          },
          {
            label: "停工",
            prop: "tg",
            align: "center",
            rules: [
              {
                required: true,
                message: "请输入停工",
                trigger: "blur"
              }
            ]
          },
          {
            label: "公司形象",
            prop: "gsxx",
            align: "center",
            rules: [
              {
                required: true,
                message: "请输入公司形象",
                trigger: "blur"
              }
            ]
          }
        ]
      }
    };
  },
  computed: {
    ...mapGetters(["permission"]),

    // 严重度标准列表权限
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.riskseverity_add, false),
        viewBtn: this.vaildData(this.permission.riskseverity_view, false),
        delBtn: this.vaildData(this.permission.riskseverity_delete, false),
        editBtn: this.vaildData(this.permission.riskseverity_edit, false)
      };
    },

    // 严重度列表选项ID
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  mounted() {
    this.onLoad();
  },
  methods: {
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad();
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        },
        error => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad();
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        },
        error => {
          loading();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad();
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad();
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    onLoad() {
      this.loading = true;
      getList().then(res => {
        this.data = res.data.data;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style></style>